package zeke.com.camera.picture;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import zeke.com.camera.control.CameraManager;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.Size;
import android.os.Environment;

public class PictureCallBack implements PictureCallback{
	
	private Bitmap bitmap = null;
	
//	public Bitmap getBitmap() {
//		if(null == bitmap){
//			Log.e("zeke.com.camera.picture.PictureCallBack", "bitmap must not null,takepicture early");
//			return null;
//		}
//		return bitmap;
//	}
	
	private Size size = null;
	public PictureCallBack(Size size){
		this.size = size;
	}

	@Override
	public void onPictureTaken(byte[] data, Camera camera) {
		// TODO Auto-generated method stub
		bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
		
		Rect frame = CameraManager.get().getFramingRect();
		Matrix matrix = new Matrix();
		float a = Float.intBitsToFloat(size.width)/Float.intBitsToFloat(bitmap.getWidth());
		float b = Float.intBitsToFloat(size.height)/Float.intBitsToFloat(bitmap.getHeight());
		matrix.preScale(a,b);
		Bitmap bitmapNew = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
		Bitmap bitmapLast = Bitmap.createBitmap(bitmapNew, frame.left, frame.top, frame.right-frame.left, frame.bottom-frame.top);
		
//		System.out.println(frame.top+","+frame.right+","+frame.bottom+","+frame.left);
		
		try {
			File file = new File(Environment.getExternalStorageDirectory(), "/xxxxxxxxx.jpg");
			bitmapLast.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	
}
